package com.bjsxt.socketdemo;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

//发送消息的线程类
class Send1 extends Thread{
    private Scanner scanner;
    private Socket socket;
    public Send1(Socket socket,Scanner scanner){
        this.socket=socket;
        this.scanner=scanner;
    }
    @Override
    public void run() {
        this.sendMsg();
    }
    private void sendMsg(){
        PrintWriter pw = null;
        try {
            pw=new PrintWriter(this.socket.getOutputStream());
            while(true){
                String msg = scanner.nextLine();
                pw.println(msg);
                pw.flush();
            }

        } catch (IOException e) {
            e.printStackTrace();
        }finally{

            if (pw!=null){
                pw.close();
            }
            if (this.socket!=null){
                try {
                    this.socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (this.scanner!=null){
                this.scanner.close();
            }

        }
    }
}

//接收消息的线程
class  Receive1 extends Thread{
    private Socket socket;
    public Receive1(Socket socket){
        this.socket=socket;
    }
    @Override
    public void run() {
        this.receiveMsg();
    }

    private void receiveMsg(){
        BufferedReader br=null;
        try {
            br = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
            while(true){
                String msg = br.readLine();
                System.out.println("他说："+msg);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (br!=null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (this.socket!=null){
                try {
                    this.socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

public class GoodTcp {

    public static void main(String[] args) {
        Scanner scanner =null;
        ServerSocket serverSocket = null;
        Socket socket = null;  //主线程不对socket关闭
        try{
            scanner = new Scanner(System.in);

            System.out.println("请输入：server,<port>或者：<ip>,<port>:");
            String str = scanner.nextLine();
            String[] arr = str.split(",");
            if ("server".equals(arr[0])){
                System.out.println("TCP Server Listen at"+arr[1]+"......");

                serverSocket = new ServerSocket(Integer.parseInt(arr[1]));
                socket = serverSocket.accept();
                System.out.println("连接成功");
            }else {
                //启动客户端
                socket = new Socket(arr[0],Integer.parseInt(arr[1]));
                System.out.println("连接成功");
            }
            new Send1(socket,scanner).start();
            new Receive(socket).start();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (serverSocket!=null){
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

}
